home *** CD-ROM | disk | FTP | other *** search
- /*
- * Copyright 1991, 1992, 1993, 1994, Silicon Graphics, Inc.
- * All Rights Reserved.
- *
- * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
- * the contents of this file may not be disclosed to third parties, copied or
- * duplicated in any form, in whole or in part, without the prior written
- * permission of Silicon Graphics, Inc.
- *
- * RESTRICTED RIGHTS LEGEND:
- * Use, duplication or disclosure by the Government is subject to restrictions
- * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
- * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
- * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
- * rights reserved under the Copyright Laws of the United States.
- */
- /* 9/6/88 David B. Ligon
-
- Modified dragon to include facilities for 8 bit machine.
-
- Additional variables and defines are below. Additional routines may be
- found at the end.
-
- Changed default bit map to a geometric progression so that more of the
- interior of the dragon would be visible.
-
- Added code to save and restore the existing color map.
-
-
- 6/29/89 David Ligon
- Converted to use RGB index into lookup table to get around the
- ever-changing, gluttonous window system usage of the color map
-
- This has a number of impications:
-
- 1) The dragon must be redrawn when a color change option is picked.
-
- 2) Interp becomes useless in building a continuous ramp ramp.
-
- 3) Color ramp must be reset when picking color option, and when initializing
- the program.
-
- Future enhancements should include:
-
- 1) Since we build a software ramp for eightbit anyway, we could probably
- use a patterned dithering as in ipaste;
-
- 1) Would be nice to have a crosshair to pick first point.
-
- 2) Redraw screen should be converted to use lrectwrite.
-
- 3) There is no real reason why we shouldn't allow resizing the window - this
- should be implemented.
- */
-
- #include <stdio.h>
- #include <gl.h>
- #include <math.h>
- #include <device.h>
- #include <gl/image.h>
- #include <time.h>
-
- #define COF 1024
- #define EBP_COF 56
-
- int x=0, y=0;
-
- double a, b, c, d;
- double spl, spr, spb, spt; /* space left right bottom top */
- double inq, inr;
-
- long orx, ory, sizex, sizey; /* window origin and size */
-
- int depth = 256;
-
- int res = 0;
- #define LOW 0
- #define HI 1
-
-
- int mode=0;
- #define MAP 0
- #define DRAGON 1
-
- short mx, my;
- short dev,val;
-
- short *save;
-
- int menu, color_menu;
- long XMAX, YMAX;
-
- /* new for Eclipse 8 bit machine */
- Colorindex *ebp_maparray;
- int eightbit = 0;
-
-
- abs(i) {
- if (i<0) return(-i); else return(i);
- }
-
- sgn(i) {
- if (i>0) return(1);
- if (i<0) return(-1);
- return(0);
- }
-
- main (argc, argv)
- int argc;
- char *argv[];
- {
-
- if (argc == 3) {
- sscanf(argv[1], "%lf", &inq);
- sscanf(argv[2], "%lf", &inr);
- } else {
- inq = inr = 0.0;
- }
-
- {
- char *t, *strrchr();
- winopen((t=strrchr(argv[0], '/')) != NULL ? t+1 : argv[0]);
- }
-
- winattach();
-
- singlebuffer();
- gconfig();
- shademodel(FLAT);
-
- /* new for Eclipse 8 bit machine */
- if (getgdesc(GD_BITS_NORM_SNG_RED) < 8) {
- eightbit = 1;
- ebp_maparray = (Colorindex *)malloc(1024 * sizeof(Colorindex));
- }
- XMAX = getgdesc(GD_XPMAX);
- YMAX = getgdesc(GD_YPMAX);
-
- save_colormap();
-
- color(BLACK);
- clear();
-
- qdevice(ESCKEY);
- qdevice(RIGHTMOUSE);
- qdevice(MIDDLEMOUSE);
- qdevice(LEFTMOUSE);
- qdevice(WINQUIT);
- qdevice(INPUTCHANGE);
- qdevice(WINSHUT);
-
- getorigin(&orx, &ory);
- getsize(&sizex, &sizey);
-
- prefsize(sizex, sizey);
- winconstraints();
-
- save = (short *)malloc(sizey * sizex * sizeof(short));
-
- make_colors(0);
-
- make_menus();
- init_values();
-
- while (TRUE) {
-
- while (qtest()) {
-
- /* switch(dev=qread(&val)) { */
- dev = qread(&val);
- switch (dev) {
-
- case ESCKEY:
- if (val) /* ESC key DOWN, ignore */
- break;
-
- case WINQUIT:
- if (eightbit) {
- drawmode(PUPDRAW);
- color(0);
- clear();
- drawmode(NORMALDRAW);
- }
- restore_colormap();
- exit(0);
- break;
-
- case REDRAW:
- reshapeviewport();
- getorigin(&orx, &ory);
- getsize(&sizex, &sizey);
- redraw_screen();
- break;
-
- case LEFTMOUSE:
- if (val) make_new_space();
- break;
-
- case MIDDLEMOUSE:
- if (mode == MAP && val) {
- select_point();
- mode=DRAGON;
- init_values();
- }
- break;
-
- case RIGHTMOUSE:
- if (val) {
- switch (dopup(menu)) {
-
- case 1:
- mode=MAP;
- init_values();
- break;
-
- case 2:
- save_rgb_picture();
- break;
-
- case 3:
- save_picture();
- break;
-
- case 4:
- make_colors(dopup(color_menu));
- if (eightbit)
- redraw_screen();
- break;
-
- case 5:
- if (eightbit) {
- drawmode(PUPDRAW);
- color(0);
- clear();
- drawmode(NORMALDRAW);
- }
- restore_colormap();
- exit(0);
- }
- }
- break;
- }
- }
-
- switch (mode) {
-
- case MAP:
- do_map();
- break;
-
- case DRAGON:
- do_dragon();
- break;
-
- }
- }
- }
-
-
- init_values() {
-
- int i, j;
-
- for (i=0; i<sizex; i++) for (j=0; j<sizey; j++) save[i*sizey + j] = 0;
-
- x=0; y=0;
-
- if (mode==MAP) {
-
- spr = 2.2;
- spl = -0.8;
- spt = 1.5 * (float)sizey/(float)sizex;
- spb = -1.5 * (float)sizey/(float)sizex;
-
- } else {
-
- spl = -2.0;
- spr = 2.0;
- spt = 2.0 * (float)sizey/(float)sizex;
- spb = -2.0 * (float)sizey/(float)sizex;
- }
-
- res = LOW;
- }
-
- save_rgb_picture() {
-
- if (saveRGBscreen("dragon.rgb", sizex, sizey)) {
-
- fprintf(stderr, "dragon: can't save image\n");
- }
-
- }
-
-
- save_picture() {
-
- if (savescreen("dragon.img", sizex, sizey)) {
-
- fprintf(stderr, "dragon: can't save image\n");
- }
-
- }
-
- redraw_screen() {
-
- int i, j;
-
- if (eightbit) {
- for (i=0; i<sizex; i+=3) for (j=0; j<sizey; j+=3) {
- color(ebp_maparray[save[i * sizey + j]]);
- rectfi(i,j,i + 2,j + 2);
- }
- } else {
- for (i=0; i<sizex; i+=3) for (j=0; j<sizey; j+=3) {
- color(save[i*sizey + j] + COF);
- rectfi(i, j, i+2, j+2);
- }
- }
-
- if (res == HI) {
- if (eightbit) {
- for (i=0; i<x; i++) for (j=0; j<sizey; j++) {
- color (ebp_maparray[save[i * sizey + j]]);
- pnt2i(i,j);
- }
- for ( i = x, j = 0; j < y; j++) {
- color (ebp_maparray[save[i * sizey + j]]);
- pnt2i(i,j);
- }
- } else {
- for (i=0; i<x; i++) for (j=0; j<sizey; j++) {
- color (save[i * sizey + j] + COF);
- pnt2i(i,j);
- }
- for ( i = x, j = 0; j < y; j++) {
- color (save[i * sizey + j] + COF);
- pnt2i(i,j);
- }
- }
- }
-
- }
-
- make_new_space()
- {
-
- long mx, my, omx, omy, cx, cy, bx, by, obx, oby;
- float corner1x, corner1y, corner2x, corner2y;
-
- if (eightbit) {
- drawmode(PUPDRAW);
- mapcolor(1, 255, 255, 255);
- } else
- drawmode(OVERDRAW);
-
- qreset();
-
- omx = mx = getvaluator(MOUSEX) - orx;
- omy = my = getvaluator(MOUSEY) - ory;
-
- obx = bx = cx = mx;
- oby = by = cy = my;
-
- corner1y = (double) my / (double) sizey *(spt - spb) + spb;
- corner1x = (double) mx / (double) sizex *(spr - spl) + spl;
-
- color(0);
- clear();
-
-
- while (!qtest()) {
-
- mx = getvaluator(MOUSEX) - orx;
- my = getvaluator(MOUSEY) - ory;
-
- if (mx != omx && my != omy) {
-
- if (abs(mx - cx) * sizey / sizex > abs(my - cy)) {
- bx = mx;
- by = cy + abs(mx - cx) * sgn(my - cy) * sizey / sizex;
- } else {
- by = my;
- bx = cx + abs(my - cy) * sgn(mx - cx) * sizex / sizey;
- }
-
- color(0);
- recti(cx, cy, obx, oby);
-
- color(1);
- recti(cx, cy, bx, by);
-
- obx = bx;
- oby = by;
- omx = mx;
- omy = my;
- }
- }
-
- if (qtest() != LEFTMOUSE)
- return;
- else
- qread(&val);
-
- if (by != cy && bx != cx) {
- corner2y = (double) by / (double) sizey *(spt - spb) + spb;
- corner2x = (double) bx / (double) sizex *(spr - spl) + spl;
-
- /* if (corner2y != corner1y && corner2x != corner1x) { */
-
- if (corner2y < corner1y) {
- spb = corner2y;
- spt = corner1y;
- } else {
- spb = corner1y;
- spt = corner2y;
- }
-
- if (corner2x < corner1x) {
- spl = corner2x;
- spr = corner1x;
- } else {
- spl = corner1x;
- spr = corner2x;
- }
-
- x = 0;
- y = 0;
- res = LOW;
- }
-
- color(0);
- clear();
-
- drawmode(NORMALDRAW);
-
- }
-
-
- make_menus() {
-
- menu = defpup("DRAGON%t|reset|save RGB image|save map image|colors|exit");
-
- color_menu = defpup("COLOR MENU%t|shell|fire|stripe|rainbow");
-
- /* color_menu = defpup("COLOR MENU%t|shell|fire|stripe"); */
-
- }
-
-
- make_colors(i)
- int i;
- {
- int j;
- int ramps, c, index;
-
- if (eightbit)
- ebp_makemap();
-
- switch (i) {
-
- case 2:
- makerange(0, 255, 0, 255, 0, 0, 0, 0);
- makerange(256, 511, 255, 255, 0, 255, 0, 0);
- makerange(512, 767, 255, 255, 255, 0, 0, 255);
- makerange(768, 1023, 255, 0, 0, 0, 255, 255);
- break;
-
- case 3:
- if (eightbit) {
- for (j = 0; j < 128; j++) {
- ebp_maparray[j * 2] = (short)rgbi(j * 2, 0 ,255);
- ebp_maparray[j * 2 + 1] = (short)rgbi(0, j * 2, 0);
- }
- } else {
- for (j = 0; j < 128; j++) {
- mapcolor(j * 2 + COF, j * 2, 0, 255);
- mapcolor(j * 2 + COF + 1, 0, j * 2, 0);
- }
- }
- makerange(256, 511, 0, 255, 255, 0, 0, 255);
- makerange(512, 767, 255, 0, 0, 255, 255, 255);
- makerange(768, 1023, 0, 255, 255, 255, 255, 255);
- break;
-
- case 4:
- index = 0;
- if (eightbit) {
- for (ramps = 0; ramps < 128; ramps ++) {
- for (c = 0; c < 8; c ++) {
- ebp_maparray[index++] =
- (short)rgbi((c&1)*255,((c&2)>>1)*255,
- ((c&4)>>2)*255);
- }
- }
- } else {
- for (ramps = 0; ramps < 128; ramps ++) {
- for (c = 0; c < 8; c ++) {
- mapcolor(COF+index++,(c&1)*255,((c&2)>>1)*255,
- ((c&4)>>2)*255);
- }
- }
- }
- break;
-
- default: /* changed to geometric progression - dbl */
- /* Make a geometric progression (n2 = n1 * 1.3; n3 = n2 * 1.3 ...) */
- makerange(0, 14, 0, 0, 0, 0, 0, 0);
- makerange(15, 19, 0, 0, 200, 0, 130, 0);
- makerange(20, 25, 0, 0, 150, 0, 160, 0);
- makerange(26, 33, 0, 0, 100, 0, 190, 0);
- makerange(34, 43, 0, 0, 50, 0, 220, 0);
- makerange(44, 56, 50, 0, 0, 0, 250, 0);
- makerange(57, 73, 100, 0, 0, 0, 255, 0);
- makerange(74, 96, 150, 0, 0, 0, 255, 0);
- makerange(97, 125, 200, 0, 0, 0, 255, 0);
- makerange(126, 162, 255, 0, 0, 0, 200, 0);
- makerange(163, 211, 255, 0, 0, 0, 150, 0);
- makerange(212, 275, 255, 0, 0, 0, 100, 0);
- makerange(276, 358, 255, 0, 0, 0, 50, 0);
- makerange(359, 465, 255, 0, 0, 0, 0, 0);
- makerange(466, 605, 170, 0, 85, 0, 0, 0);
- makerange(606, 787, 85, 0, 170, 0, 0, 0);
- makerange(788, 1023, 0, 0, 255, 0, 0, 0);
-
- break;
-
- }
-
- if (!eightbit) {
- overlay(2);
- gconfig();
- drawmode(OVERDRAW);
- mapcolor(1, 255, 255, 255);
- drawmode(NORMALDRAW);
- }
- }
-
-
- makerange(a,b,r1,r2,g1,g2,b1,b2)
- int a,b;
- int r1,r2,g1,g2,b1,b2;
- {
- int i;
- int j;
- float dr, dg, db;
-
- if (a != b) {
- i = b - a;
- dr = (float)(r2 - r1) / (float)i;
- dg = (float)(g2 - g1) / (float)i;
- db = (float)(b2 - b1) / (float)i;
-
- if (eightbit) {
- for (j = 0; j <= i; j++) {
- ebp_maparray[j + a] = (Colorindex)rgbi(
- (int)(dr * (float) j + r1),
- (int)(dg * (float) j + g1),
- (int)(db * (float) j + b1));
- }
- } else {
- a += COF;
- for (j = 0; j <= i; j++) {
- mapcolor((Colorindex)(j + a),
- (RGBvalue)(dr * (float) j + r1),
- (RGBvalue)(dg * (float) j + g1),
- (RGBvalue)(db * (float) j + b1));
- }
- }
-
- } else {
- if (eightbit) {
- ebp_maparray[a] = (Colorindex)(rgbi((int)r1, (int)g1, (int)b1));
- } else {
- mapcolor((Colorindex)(a + COF),
- (RGBvalue)r1, (RGBvalue)g1, (RGBvalue)b1);
- }
- }
- }
-
-
- select_point() {
-
- int mx, my, omx, omy;
-
- if (eightbit) {
- drawmode(PUPDRAW);
- mapcolor(1,255,255,255);
- } else
- drawmode(OVERDRAW);
-
- while (!qtest()) {
-
- mx = getvaluator(MOUSEX)-orx;
- my = getvaluator(MOUSEY)-ory;
-
- if (mx!=omx || my!=omy) {
-
- color(0);
- move2i(0,omy); draw2i(sizex,omy);
- move2i(omx,0); draw2i(omx,sizey);
-
- color(1);
- move2i(0, my); draw2i(sizex, my);
- move2i(mx, 0); draw2i(mx, sizey);
-
- omx=mx; omy=my;
- }
- }
-
- color(0);
- clear();
-
- drawmode(NORMALDRAW);
-
- a=((double)mx/(double)sizex) * (spr-spl) + spl;
- b=((double)my/(double)sizey) * (spt-spb) + spb;
- }
-
-
- do_map()
- {
- register int colur;
- register double q, r, nq, nr, q2, r2;
-
- for (; x < sizex && res == LOW && !qtest(); x += 3) {
- for (; y < sizey; y += 3) {
-
- q = inq;
- r = inr;
- colur = 0;
- c = ((double) x / (double) sizex) * (spr - spl) + spl;
- d = ((double) y / (double) sizey) * (spt - spb) + spb;
-
- do {
-
- q2 = q * q;
- r2 = r * r;
- nq = q2 - r2 - c;
- nr = 2 * q * r - d;
-
- q = nq;
- r = nr;
-
- } while (q2 + r2 < 100.0 && ++colur < 256);
-
- if (colur == 256)
- colur = 0;
-
- save[x * sizey + y] = colur;
-
- if (eightbit)
- color(ebp_maparray[colur]);
- else
- color(colur + COF);
- rectf(x, y, x + 2, y + 2);
- }
- y = 0;
- }
-
- if (!qtest() && x >= sizex && res == LOW) {
- res = HI;
- x = 0;
- y = 0;
- }
- for (; x < sizex && res == HI && !qtest(); x++) {
- for (; y < sizey; y++) {
- register int colur;
- register double q, r, nq, nr, q2, r2;
-
- q = inq;
- r = inr;
- colur = 0;
- c = ((double) x / (double) sizex) * (spr - spl) + spl;
- d = ((double) y / (double) sizey) * (spt - spb) + spb;
-
- do {
-
- q2 = q * q;
- r2 = r * r;
- nq = q2 - r2 - c;
- nr = 2 * q * r - d;
-
- q = nq;
- r = nr;
-
- } while (q2 + r2 < 100.0 && ++colur < 1024);
-
-
- if (colur == 1024)
- colur = 0;
- save[x * sizey + y] = colur;
-
- if (eightbit)
- color(ebp_maparray[colur]);
- else
- color(colur + COF);
-
- pnt2i(x, y);
- }
- y = 0;
- }
- }
-
-
- do_dragon()
- {
-
- register int colur;
- register double q, r, nq, nr, q2, r2;
-
- for (; x < sizex && res == LOW && !qtest(); x += 3) {
- for (; y < sizey; y += 3) {
-
- colur = 0;
- q = ((double) x / (double) sizex) * (spr - spl) + spl;
- r = ((double) y / (double) sizey) * (spt - spb) + spb;
- c = a;
- d = b;
-
- do {
-
- q2 = q * q;
- r2 = r * r;
- nq = q2 - r2 - c;
- nr = 2 * q * r - d;
-
- q = nq;
- r = nr;
-
- } while (q2 + r2 < 100.0 && ++colur < 256);
-
- if (colur == 256)
- colur = 0;
- save[x * sizey + y] = colur;
-
- if (eightbit)
- color(ebp_maparray[colur]);
- else
- color(colur + COF);
-
- rectf(x, y, x + 2, y + 2);
- }
- y = 0;
- }
-
- if (!qtest() && x >= sizex && res == LOW) {
- res = HI;
- x = 0;
- y = 0;
- }
- for (; x < sizex && res == HI && !qtest(); x++) {
- for (; y < sizey; y++) {
- register int colur;
- register double q, r, nq, nr, q2, r2;
-
- colur = 0;
- q = ((double) x / (double) sizex) * (spr - spl) + spl;
- r = ((double) y / (double) sizey) * (spt - spb) + spb;
-
- do {
-
- q2 = q * q;
- r2 = r * r;
- nq = q2 - r2 - c;
- nr = 2 * q * r - d;
-
- q = nq;
- r = nr;
-
- } while (q2 + r2 < 100.0 && ++colur < 1024);
-
- if (colur == 1024)
- colur = 0;
- save[x * sizey + y] = colur;
-
- if (eightbit)
- color(ebp_maparray[colur]);
- else
- color(colur + COF);
-
- pnt2i(x, y);
- }
- y = 0;
- }
-
- }
-
-
-
- savescreen(name, sizex, sizey)
- char *name;
- int sizex, sizey;
- {
- IMAGE *image;
- long i, y;
- Colorindex buff[XMAXSCREEN];
-
- if((image = iopen(name, "w", RLE(2), 2, sizex, sizey, 0)) == NULL){
- fprintf(stderr,"dragon: couldn't open file '%s'\n",name);
- return (-1);
- }
- isetname(image,name);
- isetcolormap(image, CM_SCREEN);
-
- pushviewport();
- viewport(0, XMAX, 0, YMAX);
- pushmatrix();
- ortho2(-0.5, (float)XMAX+.5, -0.5, (float)YMAX+.5);
-
- for(y=0; y<sizey; y++) {
- cmov2i((int)0, (int)y);
- i = readpixels((short)sizex, buff);
- putrow(image,buff,y,0);
- }
- popmatrix();
-
- popviewport();
- reshapeviewport();
-
- iclose(image);
- return 0;
- }
-
-
- /* this thing reads the screen colormapped image and writes
- an RGB image file based on the contents of the colormap */
-
- saveRGBscreen(name, sizex, sizey)
- char *name;
- int sizex, sizey;
- {
- IMAGE *image;
- long i, j, y;
- Colorindex buff[XMAXSCREEN];
- Colorindex rbuff[XMAXSCREEN];
- Colorindex gbuff[XMAXSCREEN];
- Colorindex bbuff[XMAXSCREEN];
-
- Colorindex c[1024][3];
-
- if (eightbit) {
- for (i = 0; i < 256; i++) {
- getmcolor(i, &c[i][0], &c[i][1], &c[i][2]);
- }
- } else {
- for (i=0; i<1024; i++) {
- getmcolor(i+COF, &c[i][0], &c[i][1], &c[i][2]);
- }
- }
-
- if((image = iopen(name, "w", RLE(1), 3, sizex, sizey, 3)) == NULL){
- fprintf(stderr,"dragon: couldn't open file '%s'\n",name);
- return (-1);
- }
- isetname(image,name);
- isetcolormap(image, CM_NORMAL);
-
- pushviewport();
- viewport(0, XMAX, 0, YMAX);
- pushmatrix();
- ortho2(-0.5, (float)XMAX+.5, -0.5, (float)XMAX+.5);
-
- for(y=0; y<sizey; y++) {
- cmov2i((int)0, (int)y);
- i = readpixels((Colorindex)sizex, buff);
-
- for (j=0; j<i; j++) {
- if (eightbit) {
- rbuff[j] = c[buff[j]&0xFF][0];
- gbuff[j] = c[buff[j]&0xFF][1];
- bbuff[j] = c[buff[j]&0xFF][2];
- } else {
-
- rbuff[j] = c[buff[j]&0xFFFFFF-COF][0];
- gbuff[j] = c[buff[j]&0xFFFFFF-COF][1];
- bbuff[j] = c[buff[j]&0xFFFFFF-COF][2];
- }
- }
- putrow(image, rbuff, y, 0);
- putrow(image, gbuff, y, 1);
- putrow(image, bbuff, y, 2);
- }
- popmatrix();
-
- popviewport();
- reshapeviewport();
-
- iclose(image);
- return 0;
- }
-
-
- ebp_makemap()
- {
- int r, g, b;
- Colorindex i;
-
- static short green[8] = {0, 36, 72, 109, 145, 182, 218, 255};
- static short red_blue[5] = {0, 63, 127, 191, 255};
-
- i = EBP_COF;
- for (b = 0; b < 5; b++) {
- for (r = 0; r < 5; r++) {
- for (g = 0; g < 8; g++) {
- mapcolor(i++, red_blue[r], green[g], red_blue[b]);
- }
- }
- }
- }
-
- static short SavedMap[1024][3];
-
- save_colormap()
- {
- int i;
-
- if (eightbit)
- {
- for (i = 0; i < 256; i++)
- {
- getmcolor(i, SavedMap[i]+0, SavedMap[i]+1, SavedMap[i]+2);
- }
- }
- else
- {
- for (i = 0; i < 1024; i++)
- {
- getmcolor(i+COF, SavedMap[i]+0, SavedMap[i]+1, SavedMap[i]+2);
- }
- }
- }
-
- restore_colormap()
- {
- int i;
-
- if (eightbit)
- {
- for (i = 0; i < 256; i++)
- {
- mapcolor(i, SavedMap[i][0], SavedMap[i][1], SavedMap[i][2]);
- }
- }
- else
- {
- for (i = 0; i < 1024; i++)
- {
- mapcolor(i+COF, SavedMap[i][0], SavedMap[i][1], SavedMap[i][2]);
- }
- }
- }
-